package p2p.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import p2p.peer.Peer;
import p2p.peer.PeerListener;

public class UiPeer extends JFrame implements PeerListener {

	private JTextArea logging;

	public static void main(String args[]) {

		UiPeer ui = new UiPeer();
		ui.setVisible(true);

	}

	public UiPeer() {

		initUi();
		Peer.getInstance().addPeerListener(this);

		// Asking the user on which port to start the client
		String port = JOptionPane.showInputDialog(this,
				"Please enter port for peer: ", 5000);
		JOptionPane.showMessageDialog(null, "You have choosen port : " + port);

		// Test if user input for port is a valid port number
		boolean validport = false;
		while (!validport) {
			try {
				int portnumber = Integer.parseInt(port);
				if (0 < portnumber && portnumber < 65535) {
					validport = true;
				}
			} catch (NumberFormatException ne) {
				port = JOptionPane.showInputDialog(this,
						"Please choose a different port: ", 5000);
				JOptionPane.showMessageDialog(null, "You have choosen port : "
						+ port);
			}
		}
		// Create socket for the peer on the specified port
		Peer.getInstance().createSocket(port);
		
		new Thread(Peer.getInstance()).start();
	}

	private void initUi() {

		this.setSize(500, 300);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setResizable(false);
		initTabbed();
		initLogging();
	}

	private void initTabbed() {

		JTabbedPane tabs = new JTabbedPane();

		JComponent addTracker = initTrackerInfo(new JPanel());
		JComponent p2pCommand = initCommand(new JPanel());
		JComponent neighbors = initNeighbors(new JPanel());

		tabs.addTab("Tracker - Info", null, addTracker, "Tracker - Info");

		tabs.addTab("p2p - Command", null, p2pCommand, "Tracker - Info");

		tabs.addTab("Neighbors - Info", null, neighbors, "Neighbors - Info");

		this.getContentPane().add(tabs);

	}

	private JComponent initTrackerInfo(JComponent trackerInfo) {

		final JTextField ip = new JTextField("127.0.0.1", 20);
		JLabel ipLabel = new JLabel("Tracker Ip");
		JButton connect = new JButton("Connect");
		connect.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				Peer.getInstance().connectTracker(ip.getText());
			}
		});

		trackerInfo.add(ipLabel);
		trackerInfo.add(ip);
		trackerInfo.add(connect);

		return trackerInfo;

	}

	private JComponent initCommand(JComponent command) {
		JButton spawn = new JButton("Spawn");

		final JTextField ip = new JTextField("127.0.0.1", 20);
		
		spawn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// Hack for prasentation
				String[] arr = ip.getText().split(":");
				
				Peer.getInstance().spawn(arr[0],arr[1]);
			}
		});

		JButton leave = new JButton("Leave");
		leave.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Peer.getInstance().leave();
			}
		});

		command.add(ip);
		command.add(spawn);
		command.add(leave);
		return command;

	}

	private JComponent initNeighbors(JComponent neighbors) {

		return neighbors;
	}

	private void initLogging() {

		logging = new JTextArea(12, this.getContentPane().getWidth());
		
		logging.setEditable(true);

		logging.setBackground(Color.white);

		JScrollPane scrollPane = new JScrollPane(logging);
		this.getContentPane().add(scrollPane, BorderLayout.SOUTH);

	}

	@Override
	public void logPeerAction(String action) {

		String current = logging.getText();
		current += action + "\n";
		this.logging.setText(current);

	}

}
